package service

import (
	"GinBlog/global"
	"GinBlog/models"
	"errors"
)

type UserService struct{}

func (u *UserService) Create(user *models.User) error {
	//验证手机号
	u1 := u.GetByPhone(*user.Phone)
	if u1.ID != 0 {
		return errors.New("手机号已经存在")
	}
	u1 = u.GetByEmail(*user.Email)
	if u1.ID != 0 {
		return errors.New("邮箱已经存在")
	}

	global.DB.Create(&user)
	return nil
}

func (u *UserService) Get(userId uint) *models.User {
	user := &models.User{}
	global.DB.First(&user, userId)
	return user
}

func (u *UserService) GetByPhone(phone string) *models.User {
	user := &models.User{}
	global.DB.Where("phone = ?", phone).First(&user)
	return user
}

func (u *UserService) GetByEmail(email string) *models.User {
	user := &models.User{}
	global.DB.Where("email = ?", email).First(&user)
	return user
}
